package indi.jp.project.core.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import indi.jp.project.core.dao.BaseDao;
import indi.jp.project.core.entity.Dictionary;
import indi.jp.project.core.entity.DictionaryGroup;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.service.DictionaryService;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictionaryServiceImpl implements DictionaryService {

    private BaseDao baseDao;

    public DictionaryServiceImpl(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public List<DictionaryGroup> queryGroupList(DictionaryGroup entity) throws SQLException {
        return baseDao.queryList("dictionaryMapper.queryGroupList", entity);
    }

    @Override
    public int saveGroup(DictionaryGroup entity) throws SQLException {
        if ("".equals(entity.getId())) {
            String code = entity.getCode();
            Object obj = baseDao.findByObject("dictionaryMapper.queryMaxBrotherNum", entity.getpId());
            int num = 0;
            if (obj != null) {
                num = (int) obj;
            }
            num = num + 1;
            String numStr = String.format("%03d", num);
            code = code + "-" + numStr;
            entity.setNum(num);
            entity.setCode(code);
            return baseDao.insert("dictionaryMapper.saveGroup", entity);
        } else {
            return baseDao.update("dictionaryMapper.eidtGroup", entity);
        }
    }

    @Override
    public int deleteGroupById(String id) throws SQLException {
        return baseDao.deleteById("dictionaryMapper.deleteGroupById", id);
    }

    @Override
    public Map<String, Object> queryGrid(Dictionary entity) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        List<Dictionary> list = baseDao.queryList("dictionaryMapper.queryList", entity);
        int count = baseDao.queryCount("dictionaryMapper.queryCount", entity);
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public int save(Dictionary entity) throws SQLException {
        if ("".equals(entity.getId()))
            return baseDao.insert("dictionaryMapper.save", entity);
        else
            return baseDao.update("dictionaryMapper.edit", entity);
    }

    @Override
    public int deleteById(String id) throws SQLException {
        return baseDao.deleteById("dictionaryMapper.deleteById", id);
    }

    @Override
    public JsonResult queryByGroups(Map<String, Object> map) throws SQLException {
        List<Dictionary> list = baseDao.queryList("dictionaryMapper.queryByCodes", map);
        if (list.size() == 0) {
            return JsonResult.failure("未能查询到数据");
        }
        JSONObject jo = new JSONObject();
        JSONArray ja = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            String code = list.get(i).getGroupCode();
            if (i == 0) {
                jo.put(code, ja);
            }
            if (jo.getJSONArray(code) == null) {
                jo.put(code, ja);
                ja = new JSONArray();
                jo.put(code, ja);
            }
            ja.add(list.get(i));
            if (i == list.size() - 1) {
                jo.put(code, ja);
            }
        }
        return JsonResult.success(jo);
    }


}
